﻿using System;
using System.Windows.Forms;

using xfsale.lib;

namespace xfsale.ui
{
    public partial class WinKey : Form
    {
        public WinKey()
        {
            InitializeComponent();
        }

        private void WinKeyMenu_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Return:
                    try
                    {
                        foreach (SysMenu menu in App.MenuSet.Values)
                        {
                            string hotKey = TxtMenuHotKey.Text.Trim();
                            if (!string.IsNullOrEmpty(hotKey) && menu.ServiceCode.EndsWith(hotKey))
                            {
                                Panel PnlMain = (Owner as WinMain).Controls.Find("PnlMain", true)[0] as Panel;
                                Label LblTitle = (Owner as WinMain).Controls.Find("LblTitle", true)[0] as Label;

                                if (PnlMain.Controls.Count > 0)
                                    return;

                                string dll = null;
                                if (!string.IsNullOrEmpty(menu.UIDll))
                                    dll = menu.UIDll;

                                FrmBase frame = Activator.CreateInstance(dll, menu.UIFrame).Unwrap() as FrmBase;
                                frame.Name = menu.Id;
                                frame.Tag = menu;
                                frame.Dock = DockStyle.Left;
                                PnlMain.Controls.Add(frame);

                                LblTitle.Text = App.MenuSet[menu.Id.Substring(0, 2)].Caption + " → "
                                    + App.MenuSet[menu.Id.Substring(0, 4)].Caption + " → "
                                    + menu.Caption;

                                break;
                            }
                        }
                    }
                    catch
                    {
                    }
                    finally
                    {
                        this.Close();
                    }
                    break;

                case Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}
